﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.WebPartPages;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
using SPWebPart = Microsoft.SharePoint.WebPartPages.WebPart;

namespace Demo.SharePoint.WebParts.StockQuote
{
    [ToolboxItemAttribute(false)]
    [Guid("3d28b168-580a-487d-a23f-9b298bed1990")]
    [XmlRoot(Namespace = "http://schemas.sorethumbmedia.com/sharepoint")]
    public class StockQuoteWebPart : SPWebPart
    {
        #region Members
        private TextBox txtStocks;
        private Button btnGetStocks;
        private SPGridView lstQuotes;
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="StockQuoteWebPart"/> class.
        /// </summary>
        public StockQuoteWebPart()
        {
            // Default properties
            Symbol = true;
            CompanyName = true;
            Last = true;
            LastTime = true;
        }

        private void DisplayStockQuotes()
        {
            EnsureChildControls();

            StockQuoteReader stockReader;
            try
            {
                List<StockQuote> quotes = new List<StockQuote>();
                List<string> symbols = new List<string>(txtStocks.Text.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries));
                stockReader = new StockQuoteReader();
                stockReader.LoadStockQuotes(symbols);
                foreach (string symbol in symbols)
                {
                    StockQuote quote = stockReader.GetStockQuote(symbol.Trim());
                    if (quote == null)
                    {
                        continue;
                    }

                    quotes.Add(quote);
                }

                lstQuotes.DataSource = quotes;
                lstQuotes.DataBind();
            }
            catch (Exception)
            {
                HtmlGenericControl div = new HtmlGenericControl("div");
                Literal litMsg = new Literal { ID = "litMsg", Text = "Could not retrieve stock quotes. Please make sure you have entered valid symbols." };
                div.Controls.Add(litMsg);
                Controls.Add(div);
                return;
            }
        }

        #region Overrides
        protected override void CreateChildControls()
        {
            HtmlGenericControl div = new HtmlGenericControl("div");
            Label lblDirections = new Label { ID = "lblDirections", Text = "Enter stock symbols separated by a comma<br/>" };
            Literal literal = new Literal { ID = "literal", Text = "&nbsp;&nbsp;&nbsp;" };
            txtStocks = new TextBox { ID = "txtStocks", ToolTip = "Enter stock symbols separated by a comma" };
            btnGetStocks = new Button { ID = "btnGetStocks", Text = "Get Stocks" };
            div.Controls.Add(lblDirections);
            div.Controls.Add(txtStocks);
            div.Controls.Add(literal);
            div.Controls.Add(btnGetStocks);
            Controls.Add(div);

            HtmlGenericControl pGrid = new HtmlGenericControl("p");
            lstQuotes = new SPGridView { ID = "lstQuotes", AutoGenerateColumns = false };
            BoundField field;
            if (Symbol)
            {
                field = new BoundField { HeaderText = "Symbol", DataField = "Symbol" };
                lstQuotes.Columns.Add(field);
            }
            if (CompanyName)
            {
                field = new BoundField { HeaderText = "Company", DataField = "CompanyName" };
                lstQuotes.Columns.Add(field);
            }
            if (Last)
            {
                field = new BoundField { HeaderText = "Last", DataField = "Last" };
                lstQuotes.Columns.Add(field);
            }
            if (LastTime)
            {
                field = new BoundField { HeaderText = "Last Time", DataField = "LastTime" };
                field.HeaderStyle.Wrap = false;
                lstQuotes.Columns.Add(field);
            }
            if (Currency)
            {
                field = new BoundField { HeaderText = "Currency", DataField = "Currency" };
                lstQuotes.Columns.Add(field);
            }
            if (Exchange)
            {
                field = new BoundField { HeaderText = "Exchange", DataField = "Exchange" };
                lstQuotes.Columns.Add(field);
            }
            if (Type)
            {
                field = new BoundField { HeaderText = "Type", DataField = "Type" };
                lstQuotes.Columns.Add(field);
            }
            if (LastSize)
            {
                field = new BoundField { HeaderText = "Last Size", DataField = "LastSize" };
                field.HeaderStyle.Wrap = false;
                lstQuotes.Columns.Add(field);
            }
            if (Ask)
            {
                field = new BoundField { HeaderText = "Ask", DataField = "Ask" };
                lstQuotes.Columns.Add(field);
            }
            if (Bid)
            {
                field = new BoundField { HeaderText = "Bid", DataField = "Bid" };
                lstQuotes.Columns.Add(field);
            }
            if (Open)
            {
                field = new BoundField { HeaderText = "Open", DataField = "Open" };
                lstQuotes.Columns.Add(field);
            }
            if (Close)
            {
                field = new BoundField { HeaderText = "Close", DataField = "Close" };
                lstQuotes.Columns.Add(field);
            }
            if (High)
            {
                field = new BoundField { HeaderText = "High", DataField = "High" };
                lstQuotes.Columns.Add(field);
            }
            if (Low)
            {
                field = new BoundField { HeaderText = "Low", DataField = "Low" };
                lstQuotes.Columns.Add(field);
            }
            if (Volume)
            {
                field = new BoundField { HeaderText = "Volume", DataField = "Volume" };
                lstQuotes.Columns.Add(field);
            }
            if (Change)
            {
                field = new BoundField { HeaderText = "Change", DataField = "Change" };
                lstQuotes.Columns.Add(field);
            }
            if (PercentChange)
            {
                field = new BoundField { HeaderText = "% Change", DataField = "PercentChange" };
                field.HeaderStyle.Wrap = false;
                lstQuotes.Columns.Add(field);
            }
            if (EPS)
            {
                field = new BoundField { HeaderText = "EPS", DataField = "EPS" };
                lstQuotes.Columns.Add(field);
            }
            if (PE)
            {
                field = new BoundField { HeaderText = "PE", DataField = "PE" };
                lstQuotes.Columns.Add(field);
            }
            if (SharesOut)
            {
                field = new BoundField { HeaderText = "Shares Out", DataField = "SharesOut" };
                field.HeaderStyle.Wrap = false;
                lstQuotes.Columns.Add(field);
            }

            pGrid.Controls.Add(lstQuotes);
            Controls.Add(pGrid);

            btnGetStocks.Click += btnGetStocks_Click;
        }
        #endregion

        #region Properties
        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Symbol")]
        [Description("If checked, displays Symbol in the results.")]
        public bool Symbol { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Company Name")]
        [Description("If checked, displays Company Name in the results.")]
        public bool CompanyName { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Last")]
        [Description("If checked, displays Last in the results.")]
        public bool Last { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Last Time")]
        [Description("If checked, displays Last Time in the results.")]
        public bool LastTime { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Currency")]
        [Description("If checked, displays Currency in the results.")]
        [DefaultValue(false)]
        public bool Currency { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Exchange")]
        [Description("If checked, displays Exchange in the results.")]
        [DefaultValue(false)]
        public bool Exchange { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Type")]
        [Description("If checked, displays Type in the results.")]
        [DefaultValue(false)]
        public bool Type { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Last Size")]
        [Description("If checked, displays Last Size in the results.")]
        [DefaultValue(false)]
        public bool LastSize { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Ask")]
        [Description("If checked, displays Ask in the results.")]
        [DefaultValue(false)]
        public bool Ask { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Bid")]
        [Description("If checked, displays Bid in the results.")]
        [DefaultValue(false)]
        public bool Bid { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Open")]
        [Description("If checked, displays Open in the results.")]
        [DefaultValue(false)]
        public bool Open { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Close")]
        [Description("If checked, displays Close in the results.")]
        [DefaultValue(false)]
        public bool Close { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display High")]
        [Description("If checked, displays High in the results.")]
        [DefaultValue(false)]
        public bool High { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Low")]
        [Description("If checked, displays Low in the results.")]
        [DefaultValue(false)]
        public bool Low { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Volume")]
        [Description("If checked, displays Volume in the results.")]
        [DefaultValue(false)]
        public bool Volume { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Change")]
        [Description("If checked, displays Change in the results.")]
        [DefaultValue(false)]
        public bool Change { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display % Change")]
        [Description("If checked, displays % Change in the results.")]
        [DefaultValue(false)]
        public bool PercentChange { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display EPS")]
        [Description("If checked, displays EPS in the results.")]
        [DefaultValue(false)]
        public bool EPS { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display PE")]
        [Description("If checked, displays PE in the results.")]
        [DefaultValue(false)]
        public bool PE { get; set; }

        [Browsable(true)]
        [Category("Symbol Settings")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("Display Shares Out")]
        [Description("If checked, displays Shares Out in the results.")]
        [DefaultValue(false)]
        public bool SharesOut { get; set; }
        #endregion

        #region Events
        private void btnGetStocks_Click(object sender, EventArgs e)
        {
            DisplayStockQuotes();
        }
        #endregion
    }
}
